'use strict';

var ctrl = module.exports = {};
var debug = require('debug'),
  log = debug('yozh:mo:menu:log'),
  error = debug('yozh:mo:menu:error'),
  verbose = debug('yozh:mo:menu:verbose'),
  service = require('./menu.service');

ctrl.find = function *(next) {
  //query data
  var query = this.request.body;
  var options = {};
  if (query.limit) {
    options.limit = query.limit;
  }
  if (query.offset) {
    options.offset = query.offset;
  }
  var menus = yield service.find(query.search, query.projection, options);
  if (menus) {
    this.status = 200;
    this.body = menus;
  } else {
    this.status = 404;
    this.body = 'not found';
  }
};

ctrl.findById = function *(next) {
  //query data
  var menu = yield service.findById(this.params.id);
  if (menu) {
    this.status = 200;
    this.body = menu;
  } else {
    this.status = 404;
    this.body = 'not found';
  }
};

/*ctrl.findOperatorsByMenuId = function *(next) {
  //query data
  var menus = yield service.findOperatorsByMenuId(this.params.id);
  if (menu) {
    this.status = 200;
    this.body = menu;
  } else {
    this.status = 404;
    this.body = 'not found';
  }
};*/

ctrl.create = function *(next) {
  //post data
  var data = this.request.body;
  var menu = yield service.create(data);
  if (menu) {
    this.status = 201;
    this.body = {message: menu};
  } else{
    this.status = 409;
    this.body = {message: 'could not create entity'};
  }
};

/*ctrl.createOperatorByMenuId = function *(next) {
  //post data
  var data = this.request.body;
  var menu = yield service.createOperatorByMenuId(this.params.id, data);
  if (menu) {
    this.status = 201;
    this.body = {};
  } else{
    this.status = 409;
    this.body = {message: 'could not create entity'};
  }
};*/

ctrl.updateById = function *(next) {
  //query data
  var data = this.request.body;
  var modify = {};
  // if (data.name !== menu.name) {
  //   modify.name = data.name;
  // }
  var menu = yield service.findByIdAndUpdate(this.params.id, modify);
  if (menu) {
    this.status = 200;
    this.body = {};
  } else {
    this.status = 409;
    this.body = {message: 'could not find an entity'};
  }
};

/*ctrl.updateOperatorByMenuId = function *(next) {
  //query data
  var data = this.request.body;
  var modify = {};
  if (data.name != menu.name) {
    modify.name = data.name;
  }
  var menu = yield service.updateOperatorByMenuId(this.params.id, modify);
  if (menu) {
    this.status = 200;
    this.body = {};
  } else {
    this.status = 409;
    this.body = {message: 'could not find an entity'};
  }
};*/

ctrl.delete = function *(next) {
  //query data
  var data = this.request.body;
  var result = yield service.remove(data);
  if (result) {
    this.status = 200;
    this.body = result;
  } else {
    this.status = 409;
    this.body = {message: 'could not find an entity'};
  }
};

ctrl.deleteById = function *(next) {
  //query data
  var data = this.request.body;
  var result = yield service.findByIdAndRemove(this.params.id);
  if (result) {
    this.status = 200;
    this.body = result;
  } else {
    this.status = 409;
    this.body = {message: 'could not find an entity'};
  }
};

/*ctrl.deleteOperatorsByMenuId = function *(next) {
  //query data
  var data = this.request.body;
  var result = yield service.removeOperatorsByMenuId(this.params.id, data);
  if (result) {
    this.status = 200;
    this.body = result;
  } else {
    this.status = 409;
    this.body = {message: 'could not find an entity'};
  }
};

ctrl.updateOperatorsByMenuId = function *(next) {
  //query data
  var data = this.request.body;
  var result = yield service.updateOperatorsByMenuId(this.params.id, data);
  if (result) {
    this.status = 200;
    this.body = result;
  } else {
    this.status = 409;
    this.body = {message: 'could not find an entity'};
  }
};*/
